home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Tech Arsenal 1
/
Tech Arsenal (Arsenal Computer).ISO
/
tek-04
/
prolog_2.zip
/
EXPERT.ZIP
/
PSYCHIAT.PRO
< prev
next >
Wrap
Text File
|
1987-05-26
|
5KB
|
156 lines
/*
A simulated psychiatrist program ----> Psychiat.PRO
contributed by John Sun, 1987 using PD Prolog version 1.91P.
*/
/*
Reading in a list of words: 13 is Carriage Return,
8 is Backspace,
32 is Space.
If you spell a word wrong, try to use the backspace key to correct it.
*/
get_Line([C|Cs]) :- get0(C), C \= 13, !, get_Line(Cs).
get_Line([13]) :- nl.
edit_Line([]).
edit_Line([X|Xs]) :- X \= 8, asserta(char(X)), !, edit_Line(Xs).
edit_Line([X|Xs]) :- X = 8, retract(char(_)), !, edit_Line(Xs).
convert_Line([Y|Ys]) :- retract(char(X)),
convert_Char(X, Y),
!,
convert_Line(Ys).
convert_Line([]).
convert_Char(13, 13).
convert_Char(X, Y) :- 65 =< X, X =< 90, Y is X + 32.
convert_Char(X, X) :- 97 =< X, X =< 122.
convert_Char(_, 32).
to_Memory([]).
to_Memory([X|Xs]) :- assertz(char(X)), to_Memory(Xs).
read_word_list(Ws) :-
retract(char(C)),
read_word_list(C, Ws).
read_word_list(C, [W|Ws]) :-
C \= 32, C \= 13,
read_word(C, W, C1),
read_word_list(C1, Ws).
read_word_list(C, Ws) :-
C = 32,
retract(char(C1)),
read_word_list(C1, Ws).
read_word_list(C, []) :-
C = 13.
read_word(C, W, C1) :-
word_chars(C, Cs, C1),
name(W, Cs).
word_chars(C, [C|Cs], C0) :-
C \= 13, C \= 32,
!,
retract(char(C1)),
word_chars(C1, Cs, C0).
word_chars(C, [], C) :-
(C = 32; C = 13).
reverse(Xs, Ys) :- reverse(Xs, [], Ys).
reverse([X|Xs], Acc, Ys) :- reverse(Xs, [X|Acc], Ys).
reverse([], Ys, Ys).
append([], X, X).
append([X|Xs], Ys, [X|Zs]) :- append(Xs, Ys, Zs).
get_word_list(Ws) :- get_Line(Xs),
edit_Line(Xs),
convert_Line(Ys),
reverse(Ys, Zs),
to_Memory(Zs),
read_word_list(Ws).
/*
match(Pattern, Dictionary, Words) :-
Pattern matches the list of words Words, and matchings are
recorded in the Dictionary.
*/
match([N|Pattern], Dictionary, Target) :-
integer(N), lookup(N, Dictionary, LeftTarget),
append(LeftTarget, RightTarget, Target),
match(Pattern, Dictionary, RightTarget).
match([Word|Pattern], Dictionary, [Word|Target]) :-
atom(Word), match(Pattern, Dictionary, Target).
match([], Dictionary, []).
/*
lookup(Key, Dictionary, Value) :-
Dictionary contains Value indexed under Key.
Dictionary is represented as a list of pairs (Key, Value).
*/
lookup(Key, [(Key,Value)|Dictionary], Value).
lookup(Key, [(Key1, Value1)|Dictionary], Value) :-
Key \= Key1, lookup(Key, Dictionary, Value).
/*
pattern(Stimulus, Response) :-
Response is an applicable response pattern to the pattern Stimulus.
*/
pattern([i,am,1],['How long have you been',1,'?']).
pattern([1,you,2,me],['What makes you think I',2,'you ?']).
pattern([i,like,1,my,2],
['Anyone else in your relationships like',1,'your',2,'?']).
pattern([i,like,1],['Does anyone else in your family like',1,'?']).
pattern([i,feel,1],['Do you often feel that way ?']).
pattern([this,is,1],['What else do you regard as',1,'?']).
pattern([1,X,2],['Can you tell me more about',X,'?']) :- important(X).
pattern([why,1,i,2],['Why',1,'you',2,'what ?']).
pattern([1,X,2,Y,3],['Why do',Y,'want to',1,X,2,3,'?']) :-
(i_you(X, Y); i_you(Y, X)).
pattern([1,X,2],['Why does',1,Y,2,'?']) :-
(i_you(X, Y); i_you(Y, X)).
pattern([1],['I see. Please go on.']).
i_you(i, you). i_you(my, your). i_you(me, you).
i_you(myself, yourself). i_you(mine, yours).
important(father). important(mother). important(son).
important(sister). important(brother). important(daughter).
important(friend). important(husband). important(wife).
important(pet). important(dog). important(job).
important(work). important(program). important(computer).
important(sickness). important(pain). important(school).
reply([Head|Tail]) :- print(Head,' '), reply(Tail).
reply([]) :- nl.
/*
help :-
Simulate a conversation via side-effects.
*/
help :- nl, print('Please tell me anything about yourself'),
nl, print('your family, your job, school, or this program.'),
nl, get_word_list(Input), help(Input), !.
help([bye|_]) :-
print('Goodbye. I hope I have helped you. ').
help([see,you|_]) :-
print('Okay, see you later. If you have any questions,'), nl,
print('please do not hesitate to call me.').
help(Input) :-
pattern(Stimulus, Response),
match(Stimulus, Dictionary, Input),
match(Response, Dictionary, Output),
reply(Output),
get_word_list(Input1),
!, help(Input1).
?-nl, print('To start the program, type "help." '),
nl, print('To terminate the conversation, type "Bye"'),
nl.